﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace CinemabookingWCFApp.UserFolder
{
    public class UserService : IUserService
    {
        #region
        CinemabookingDBDataContext db = new CinemabookingDBDataContext(); 
        #endregion

        // Service til at søge på en user, ved at indtaste id på user.
        public List<User> GetUserById(int UserId)
        {
            var queryResult = (from x in db.Users
                               where x.id == UserId
                               select x);
            return queryResult.ToList();            
        }

        // Service til at printe alle users
        public List<User> GetAllUsers()
        {
            var queryResult = (from x in db.Users 
                               select x);
            return queryResult.ToList();
        }

        // Service til at oprette en ny user i Databasen
        public void SaveUser(string UserName, string UserAddress, string UserPassword, string UserPhone, string UserEmail)
        {
            User newUser = new User{name=UserName, address=UserAddress, password=UserPassword, phonenr=UserPhone, email=UserEmail};
            db.Users.InsertOnSubmit(newUser);
            db.SubmitChanges();
        }

        // Service til at slette user i databasen
        public void DeleteUser(int UserId)
        {
            User delUser = (from x in db.Users
                            where x.id == UserId
                            select x).FirstOrDefault();
            if (delUser !=null)
            {
                db.Users.DeleteOnSubmit(delUser);
                db.SubmitChanges();
            }
        }

        // Service til at opdater oplysninger omkring en user i databasen
        public void UpdateUser(int UserId, string UserName, string UserAddress, string UserPassword, string UserPhone, string UserEmail)
        {
            User updUser = (from x in db.Users
                            where x.id == UserId
                            select x).Single();

            updUser.name = UserName;
            updUser.address = UserAddress;
            updUser.password = UserPassword;
            updUser.phonenr = UserPhone;
            updUser.email = UserEmail;

            db.SubmitChanges();
        }
    }
}